package com.clerkq.FlvPlayer
{
	
	import com.clerkq.FlvPlayer.ui.ProgressIndicator;
	import com.clerkq.FlvPlayer.ui.VolumeBar;
	import com.clerkq.SoundPlayer.Event.SoundPlayerEvent;
	import com.content.model.LoadedAssets.SkinProxy;
	import com.content.view.Component.Button.HighLightButton;
	import com.content.view.Component.Button.HighLightToggleButton;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class VideoPlayer extends UIObject implements IDisposable
	{
		// Depth ////////////////////////
		private const DEPTH_BG:Number = 0;
		private const DEPTH_FRAME:Number = 1;
		private const DEPTH_DISCLAIMERTXT:Number = 1;
		private const DEPTH_PLAYBTN:Number = 2;
		private const DEPTH_MUTEBTN:Number = 3;
		private const DEPTH_PROGRESSBAR:Number = 4;
		private const DEPTH_VOLUMEBAR:Number = 5;
		
		private var _bg:Sprite;
		private var _frame:Sprite;
		private var _disclaimerTxt:TextField;
		private var _playBtn:HighLightToggleButton;
		private var _muteBtn:HighLightButton;
		private var _progressBar:ProgressIndicator;
		private var _volumeBar:VolumeBar;
		
		
		private var _volumeBeforeMute:Number;
		
		public function VideoPlayer()
		{
			super();
			create();
			addListener();
		}
		
		private function create():void{
			addBG();
			addDisclaimerTxt();
			addButtons();
			addProgressBar();
			addVolumeBar();
		}
		
		private function addBG():void{
			// Bg and Frame
			
			_bg = new SkinProxy.VideoPlayer_PlayerBG() as Sprite;
			_frame = new SkinProxy.VideoPlayer_PlayerFrame() as Sprite;
			addChildAt(_bg, DEPTH_BG); 
			addChildAt(_frame, DEPTH_FRAME);
			_frame.x = 1.5;
			_frame.y = 2;
		}
		
		private function addDisclaimerTxt():void{
			// title text
			var txtFmt:TextFormat = new TextFormat();
				txtFmt.font = "Arial"
				txtFmt.size = 11;
				txtFmt.color = 0xffffff;
				
			_disclaimerTxt = new TextField();
			_disclaimerTxt.embedFonts = true;
			_disclaimerTxt.selectable = false;
			_disclaimerTxt.defaultTextFormat = txtFmt;
			_disclaimerTxt.autoSize = TextFieldAutoSize.LEFT;
			_disclaimerTxt.antiAliasType = AntiAliasType.ADVANCED;
			_disclaimerTxt.width = 500;
			_disclaimerTxt.text = "Unsetup";
			addChild(_disclaimerTxt);
			_disclaimerTxt.x = 60;
			_disclaimerTxt.y = 317;
		}
		
		private function addButtons():void{
			// Play Button (Play/Pause in one button)
			var playNormal:MovieClip = new SkinProxy.VideoPlayer_PlayButton() as MovieClip;
			var playLight:MovieClip = new SkinProxy.VideoPlayer_PlayButtonLight() as MovieClip;
			_playBtn = new HighLightToggleButton(playNormal, playLight);
			addChildAt(_playBtn, DEPTH_PLAYBTN);			
			_playBtn.x = 15;
			_playBtn.y = 430;
			
			// Mute Button
			var muteNormal:Sprite = new SkinProxy.VideoPlayer_Mute() as Sprite;
			var muteLight:Sprite = new SkinProxy.VideoPlayer_MuteLight() as Sprite;
			_muteBtn = new HighLightButton(muteNormal, muteLight);
			addChildAt(_muteBtn, DEPTH_MUTEBTN);			
			_muteBtn.x = 413;
			_muteBtn.y = 448;
		}
		
		private function addProgressBar():void{
			_progressBar = new ProgressIndicator();
			addChildAt(_progressBar, DEPTH_PROGRESSBAR);			
			_progressBar.x = 70;
			_progressBar.y = 445;
		}
		
		private function addVolumeBar():void{
			_volumeBar = new VolumeBar();
			_volumeBar.percent = 0.5;
			addChildAt(_volumeBar, DEPTH_VOLUMEBAR);			
			_volumeBar.x = 435;
			_volumeBar.y = 451;
		}
		
		public function Dispose():void
		{
			// 1. Remove listener
			removeListener();
			
			// 2. Dispose all
			//_bg;
			//_frame;
			//disclaimerTxt;
			_playBtn.Dispose();
			_muteBtn.Dispose();
			_progressBar.Dispose();
			_volumeBar.Dispose();
			
			// 3. Remove all
			removeAllChilds();
		}
		
		private function addListener():void{
			_playBtn.addEventListener(MouseEvent.CLICK, onControlBtnClick);
			_muteBtn.addEventListener(MouseEvent.CLICK, onControlBtnClick);
			_muteBtn.addEventListener(MouseEvent.CLICK, onSoundSwitch);
			_volumeBar.addEventListener(VolumeBar.SOUND_CHANGE, onSoundChange);
		}
		
		private function removeListener():void{
			_playBtn.removeEventListener(MouseEvent.CLICK, onControlBtnClick);
			_muteBtn.removeEventListener(MouseEvent.CLICK, onControlBtnClick);
			_muteBtn.removeEventListener(MouseEvent.CLICK, onSoundSwitch);
			_volumeBar.removeEventListener(VolumeBar.SOUND_CHANGE, onSoundChange);
		}
		
		// EVENT /////////////////////////////////////////////////////
		private function onControlBtnClick(evt:MouseEvent):void{
			switch(evt.target)
			{
				case _playBtn:
					if(_playBtn.isToggle){
						dispatchEvent(new Event(SoundPlayerEvent.SOUND_PLAYER_PLAY));
					}else{
						dispatchEvent(new Event(SoundPlayerEvent.SOUND_PLAYER_PAUSE));
					}
					break;
				case _muteBtn:
					break;
			}
		}
		
		private function onSoundSwitch(evt:MouseEvent):void
		{
			if(_volumeBar.percent != 0){
				// Mute
				_volumeBeforeMute = _volumeBar.percent;
				_volumeBar.percent = 0;
				dispatchEvent(new Event(SoundPlayerEvent.SOUND_CHANGE));
			}else{
				// Unmute (Back to previous value)
				_volumeBar.percent = _volumeBeforeMute;
				dispatchEvent(new Event(SoundPlayerEvent.SOUND_CHANGE));
			}
		}
		
		private function onSoundChange(evt:Event):void
		{
			dispatchEvent(new Event(SoundPlayerEvent.SOUND_CHANGE));
		}
		
		// GET AND SET ///////////////////////////////////////////////
		public function set playStatus(playStatus:Boolean):void{
            _playBtn.isToggle = playStatus;
        }
		public function get volumeBar():VolumeBar			{		return _volumeBar;			}
		public function get progressBar():ProgressIndicator		{		return _progressBar;		}
		
	}
}